//package com.yfbao.horizon.user.injection.config;
//
//import com.alibaba.druid.DbType;
//import com.alibaba.druid.pool.DruidDataSource;
//import com.zaxxer.hikari.HikariDataSource;
//import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
//import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
//import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
//import org.mybatis.spring.annotation.MapperScan;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.boot.jdbc.DataSourceBuilder;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//import org.springframework.transaction.annotation.EnableTransactionManagement;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
///**
// * 数据库配置写在代码中，实现了用户手动注入的方式构建基于sharding的dataSource
// * 用户在配置文件中没有任何数据库相关的配置
// */
//@Configuration
//public class ShardingDataSourceConfig {
//
//    private Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    @Autowired
//    private Environment env;
//
//    @Bean
//    public DataSource getShardingDataSource() throws SQLException {
//        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//        shardingRuleConfig.getTableRuleConfigs().add(getUserTableRuleConfiguration());
//        shardingRuleConfig.getTableRuleConfigs().add(getTestTableRuleConfiguration());
////        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
////        shardingRuleConfig.getBroadcastTables().add("t_config");
//        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("id", "ds${id % 2}"));
////        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new ModuloShardingTableAlgorithm()));
//
//        Properties prop = new Properties();
//        prop.setProperty("sql.show",Boolean.TRUE.toString());
//        return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, prop);
//    }
//
//    private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
//        KeyGeneratorConfiguration result = new KeyGeneratorConfiguration("SNOWFLAKE", "id");
//        return result;
//    }
//
//    TableRuleConfiguration getUserTableRuleConfiguration() {
//        TableRuleConfiguration result = new TableRuleConfiguration("user", "ds${0..1}.user_${0..2}");
//        result.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("id", "user_${id % 3}"));
//        result.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("id", "ds${id % 2}"));
//        result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
//        return result;
//    }
//
//    TableRuleConfiguration getTestTableRuleConfiguration() {
//        TableRuleConfiguration result = new TableRuleConfiguration("test", "ds${0..1}.test_${0..2}");
//        result.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("table_index", "test_${table_index % 3}"));
//        result.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("table_index", "ds${table_index % 2}"));
//        return result;
//    }
//
//    Map<String, DataSource> createDataSourceMap() {
//        Map<String, DataSource> result = new HashMap<>();
//        result.put("ds0", createDruidDataSource0());
//        result.put("ds1", createDruidDataSource());
//        return result;
//    }
//
//    private DataSource createHikariDataSource(){
//        HikariDataSource result = new HikariDataSource();
//        result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
//        result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8", "localhost", "3306", "ds_0"));
//        result.setUsername("root");
//        result.setPassword("root");
//        return result;
//    }
//
//    private DataSource createDruidDataSource0(){
//        HikariDataSource result = new HikariDataSource();
//        result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
//        result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8", "localhost", "3306", "ds_0"));
//        result.setUsername("root");
//        result.setPassword("root");
//        return result;
//    }
//
//    private DataSource createDruidDataSource(){
//        DruidDataSource result = new DruidDataSource();
//        result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
//        result.setUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8", "localhost", "3306", "ds_1"));
//        result.setUsername("root");
//        result.setPassword("root");
//        return result;
//    }
//
//
////    public static DataSource buildDataSource(Map<String, Object> dataSourceMap) {
////        Object type = dataSourceMap.get("type");
////        if (type == null) {
////            type = DATASOURCE_TYPE_DEFAULT;
////        }
////        try {
////            Class<? extends DataSource> dataSourceType;
////            dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);
////            //String driverClassName = dataSourceMap.get("driver").toString();
////            String url = dataSourceMap.get("url").toString();
////            String username = dataSourceMap.get("username").toString();
////            String password = dataSourceMap.get("password").toString();
////            // 自定义DataSource配置
////            DataSourceBuilder factory = DataSourceBuilder.create().url(url).username(username).password(password).type(dataSourceType);
////            return factory.build();
////        } catch (Exception e) {
////            log.error("构建数据源" + type + "出错", e);
////        }
////        return null;
////    }
//
//}
